package leetcode.code0831;

import java.util.StringJoiner;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public String maskPII(String s) {
		if (s.indexOf('@') >= 0) {
			return this.email(s);
		}
		return this.phone(s);
	}

	private String email(String s) {
		String[] str = s.split("@");
		String name = str[0].toLowerCase();
		StringBuffer sb = new StringBuffer();
		sb.append(name.subSequence(0, 1)).append("*****").append(name.substring(name.length() - 1)).append("@")
				.append(str[1].toLowerCase());
		return sb.toString();
	}

//	"***-***-XXXX" 如果国家代码为 0 位数字
//	"+*-***-***-XXXX" 如果国家代码为 1 位数字
//	"+**-***-***-XXXX" 如果国家代码为 2 位数字
//	"+***-***-***-XXXX" 如果国家代码为 3 位数字
	private String phone(String s) {
		char[] cs = s.toCharArray();
		int len = 0;
		for (int i = 0; i < cs.length; i++) {
			char c = cs[i];
			if (c >= '0' && c <= '9') {
				cs[len++] = c;
			}
		}
		StringBuffer sb = null;
		if (len == 10) {
			sb = new StringBuffer("***-***-");
		} else if (len == 11) {
			sb = new StringBuffer("+*-***-***-");
		} else if (len == 12) {
			sb = new StringBuffer("+**-***-***-");
		} else {
			sb = new StringBuffer("+***-***-***-");
		}
		sb.append(new String(cs, len - 4, 4));
		return sb.toString();
	}

	@Override
	public void debug4() {
		H.compare("l*****e@leetcode.com", this.maskPII("LeetCode@LeetCode.com"));
		H.compare("***-***-7890", this.maskPII("1(234)567-890"));
		H.compare("+**-***-***-5678", this.maskPII("86-(10)12345678"));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
